
#include "stdafx.h"
#include "Win32.Data.Odbc.h"
#include "Win32.DateTime.h"

void TestOdbcClientFetchRecords()
{
   // prepare datasource
   Kif::Win32::Data::DataSourceInfo dsrc;
   dsrc.SetSourceName(_T("DataDb"));
   dsrc.AuthenticationOption = Kif::Win32::Data::Authentication_NT;

   // connect to database
   Kif::Win32::Data::OdbcClient::OdbcConnection cnt;
   cnt.Database.Verbose = Kif::Win32::Diagnostics::Verbose_Trace;
   if (cnt.Connect(dsrc))
   {
      Kif::Win32::Data::OdbcClient::OdbcCommand cmd(
         &cnt,_T("{ CALL SubmissionRegistryGetRecords (?) }"),
         Kif::Win32::Data::Command_StoredProcedure);

      TCHAR *sid  = _T("01234");
      TCHAR *estr = _T("");
      cmd.Parameters.Add(_T("@SubmissionId"),sid,20);

      Kif::Win32::Data::OdbcClient::OdbcParameter *c1,*c2;
      Kif::Win32::TIMESTAMP_STRUCT dtime;
      //cmd.Columns.Add(_T("CreatedDate",&dtime);
      c1 = cmd.Columns.Add(_T("SubmissionId"),estr,20);
      c2 = cmd.Columns.Add(_T("SubmissionFileName"),estr,20);

      Kif::Win32::Data::OdbcClient::OdbcDataReader *r = cmd.OpenReader();
      if (r != NULL)
      {
         while(r->Read())
         {
            Kif::Win32::Diagnostics::Log::ConsoleWriteMessage(c1->AsWideString());
            Kif::Win32::Diagnostics::Log::ConsoleWriteMessage(c2->AsWideString());
         }
      }

      cmd.Dispose();
      cnt.Disconnect();
   }
}  // end of TestOdbcClientFetchRecords

void TestOdbcClient()
{
   // prepare datasource
   Kif::Win32::Data::DataSourceInfo dsrc;
   dsrc.SetSourceName(_T("DataDb"));
   dsrc.AuthenticationOption = Kif::Win32::Data::Authentication_NT;

   // connect to database
   Kif::Win32::Data::OdbcClient::OdbcConnection cnt;
   cnt.Database.Verbose = Kif::Win32::Diagnostics::Verbose_Trace;
   if (cnt.Connect(dsrc))
   {
      Kif::Win32::Data::OdbcClient::OdbcCommand cmd(
         &cnt,_T("{ ?= CALL SubmissionRegistryInsertRecord (?,?) }"),
         Kif::Win32::Data::Command_StoredProcedure);

      //Kif::Win32::Data::OdbcClient::OdbcCommand cmd(
      //   &cnt,_T("INSERT INTO SubmissionRegistry (SubmissionId, SubmittedFileName) VALUES (?,?)"),
      //   Kif::Win32::Data::Command_StoredProcedure);

      long rval = 0;
      TCHAR *sid  = _T("01234");
      TCHAR *path = _T("01234567");

      Kif::Win32::Data::OdbcClient::OdbcParameter *p =
         cmd.Parameters.Add(_T("@RETURN_VALUE"),rval);
      p->Direction = Kif::Win32::MemoryParamDirection_Output;

      cmd.Parameters.Add(_T("@SubmissionId"),sid,20);
      cmd.Parameters.Add(_T("@SubmittedFileName"),path,20);

      cmd.ExecuteNonQuery();
      rval = p->AsLong();
      cmd.Dispose();

      cnt.Disconnect();
   }
}  // end of TestOdbcClient

/*
extern void TestOdbcOracleAccess();

void TestOdbcOracleAccess2()
{
   Kif::Win32::Data::DataSourceInfo DataSource;
   Kif::Win32::Data::OdbcClient::OdbcConnection DbConnection;

   DataSource.SourceName.Copy(_T("CafisDb"));
   DataSource.Catalog.Copy(_T(""));
   DataSource.UserId.Copy(_T("cafis"));
   DataSource.Password.Copy(_T("cafis11"));

   if (!DbConnection.Connect(DataSource))
      return;

   static SQLTCHAR *sqlStmt = _T("SELECT Trans_No, TOT FROM af_status");
   SQLTCHAR   ename[50];
   SQLTCHAR     job[50];
   SQLINTEGER enamelen, joblen;
    
   _tprintf(_T("Retrieve ENAME and JOB using SQLBindCol 1.../n[%s]/n"), sqlStmt);
    
   // Step 1: Prepare and Execute

   SQLHENV  envHnd  = DbConnection.Database.hEnvironment;
   SQLHDBC  conHnd  = DbConnection.Database.hDatabaseConnection;
 
   SQLHSTMT stmtHnd;
   SQLRETURN rc = SQLAllocHandle(SQL_HANDLE_STMT,conHnd,&stmtHnd);

   //rc = SQLExecDirect(stmtHnd, sqlStmt, SQL_NTS); // select
   rc = SQLPrepare(stmtHnd,sqlStmt,SQL_NTS);
   rc = SQLExecute(stmtHnd);
   //checkSQLErr(envHnd, conHnd, stmtHnd, rc);
    
   // Step 2: Bind Columns
   SQLINTEGER enameSize = sizeof(ename);
   rc = SQLBindCol(stmtHnd,
                   1,
                   SQL_C_TCHAR,
                   ename,
                   enameSize,
                   &enamelen);
   //checkSQLErr(envHnd, conHnd, stmtHnd, rc);
    
   rc = SQLBindCol(stmtHnd,
                   2,
                   SQL_C_TCHAR,
                   job,
                   sizeof(job),
                   &joblen);
   //checkSQLErr(envHnd, conHnd, stmtHnd, rc);
    
   do 
   {
     // Step 3: Fetch Data
     rc = SQLFetch(stmtHnd);
     if (rc == SQL_NO_DATA)
       break;
     //checkSQLErr(envHnd, conHnd, stmtHnd, rc);
     _tprintf(_T("ENAME = %s, JOB = %s/n"), ename, job);
   } while (1);
   _tprintf(_T("Finished Retrieval/n/n"));

   DbConnection.Disconnect();
}

void TestOdbcOracleAccess()
{
   //TestOdbcOracleAccess2();
   Kif::Win32::Data::DataSourceInfo DataSource;
   Kif::Win32::Data::OdbcClient::OdbcConnection DbConnection;

   DataSource.SourceName.Copy(_T("CafisDb"));
   DataSource.Catalog.Copy(_T(""));
   DataSource.UserId.Copy(_T("cafis"));
   DataSource.Password.Copy(_T("cafis11"));

   if (DbConnection.Connect(DataSource))
   {
      Kif::Win32::Data::OdbcClient::OdbcCommand cmd(
         &DbConnection,
         _T("SELECT Trans_No, TOT, TCN, TCR FROM af_status ")
         _T("WHERE TOT=? AND Trans_Time>? ORDER BY Trans_Time"),
         Kif::Win32::Data::Command_Query);

      long rval = 0;
      TCHAR *car  = _T("CAR");
      TCHAR *cutDate = _T("1-JUL-2007");

      Kif::Win32::Data::OdbcClient::OdbcColumn *c1,*c2,*c3,*c4;
      Kif::Win32::TIMESTAMP_STRUCT dtime;

      TCHAR transNo[41];
      TCHAR tot[4];
      TCHAR tcn[41];
      TCHAR tcr[41];
      TCHAR *estr = _T("");

      cmd.Parameters.Add(_T(":TOT"),car,3);
      cmd.Parameters.Add(_T(":Trans_Time"),cutDate,10);

      Kif::Win32::Data::OdbcClient::OdbcDataReader *r = cmd.OpenReader();
      if (r != NULL)
      {

         // in Oracle the Declaration/Binding of Columns should be after preparing
         // the statement...
         c1 = cmd.Columns.Add(_T("Trans_No"),transNo,41);
         c2 = cmd.Columns.Add(_T("TOT"),tot,4);
         c3 = cmd.Columns.Add(_T("TCN"),tcn,41);
         c4 = cmd.Columns.Add(_T("TCR"),tcr,41);

         cmd.BindColumns(cmd.GetStatementHandle(),cmd.Columns);

         while(r->Read())
         {
            Kif::Win32::Diagnostics::Log::ConsoleWriteMessage(c1->AsWideString());
            Kif::Win32::Diagnostics::Log::ConsoleWriteMessage(c2->AsWideString());
            Kif::Win32::Diagnostics::Log::ConsoleWriteMessage(c3->AsWideString());
            Kif::Win32::Diagnostics::Log::ConsoleWriteMessage(c4->AsWideString());
         }
         //delete r;
      }

      cmd.Dispose();

      DbConnection.Disconnect();
   }
}

*/

